const app = getApp()
import {
    requestApi
} from '../../api'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        title: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // wx.showModal({
        //   content: JSON.stringify(options)
        // })
        // return;
        
        
        options.booking_source = 10;
        options.channel = 8;
        let that = this;
        wx.login({
            success(res) {
                app.request({
                    url: requestApi.getKey,
                    method: 'POST',
                    login: false,
                    data: {
                        js_code: res.code
                    },
                }, (data) => {
                    if (data.original.ret_code == 200) {
                        options.open_id = data.original.data.openid;
                        if(options.tradeNo){
                            // 已有订单 待付款支付
                            that.weixinPayTradeNo(options.tradeNo, data.original.data.openid)
                        }else{
                            wx.showLoading({
                                title: '加载中..'
                            })
                            app.request({
                                url: requestApi.payUrl,
                                method: 'POST',
                                data: options,
                            }, (data) => {
                                let tradeNo = data.original.trade_no;
                                that.data.paySign = data.original.wechat_pay_detail;
                                that.weixinPay(tradeNo)
                            }, (error) => {
                                that.orderStatus = false
                                wx.showModal({
                                    title: '提示',
                                    showCancel: false,
                                    content: error,
                                })
                            }, () => {
    
                            })
                        }
                        
                    }
                }, (error) => {
                    wx.showModal({
                        title: '提示',
                        showCancel: false,
                        content: error,
                    })
                })
            }
        })
        
    },

    onHide() {
        this.setData({
            title: ''
        })
    },

    weixinPayTradeNo(tradeNo, open_id) { // TODO 确认订单页、订单详情页，这两个页面的支付逻辑统一处理
        let params = {
            trade_no: tradeNo,
            channel: 8,
            open_id: open_id
        }

        this.setData({
            loading: false,
            loadingText: '正在支付中...'
        })

        app.request({
            url: requestApi.weixinPayUrlAgent,
            method: 'POST',
            data: params,
        }, (data) => {
            this.setData({
                paySign: data.original.wechat_pay_detail
            })

            this.paymentHandler(tradeNo, 'requestPayment');
        }, (error) => {
            wx.hideLoading()
            wx.showModal({
                title: '提示',
                showCancel: false,
                content: error,
            })
        }, () => {
            wx.hideLoading()
            this.payStatus = false
            // this.resetLoadingSettings()
        })
    },




    weixinPay(tradeNo) { // TODO 确认订单页、订单详情页，这两个页面的支付逻辑统一处理
        // this.setData({
        //   loading: false,
        //   loadingText: '正在支付中...'
        // })

        this.payStatus = true;

        this.paymentHandler(tradeNo, 'requestPayment');

        this.payStatus = false
        // this.resetLoadingSettings()
    },



    paymentHandler(trade_no, paymentType, successCallback) {
        let signData = this.data.paySign || {};
        wx.hideLoading()
        delete signData['status']
        delete signData['appId']

        // 微信支付成功回调
        signData.success = () => {
            this.setData({
                title: '支付成功'
            })
            // 回调通知接口
            app.request({
                // url: WEIXIN_PAY_FRONT_CALLBACK,
                url: requestApi.weixinPayCallback,
                method: 'POST',
                data: {
                    trade_no
                }
            }, (data) => {

                // successCallback && successCallback()
            }, (error) => {

            }, () => {
                // successCallback && successCallback()
            })
        }

        // 微信支付失败回调
        signData.fail = (error) => {
            if (error.errMsg == paymentType + ':fail cancel') { // 用户取消支付
                this.setData({
                    title: '支付失败'
                })
            } else { // 调用支付失败 TODO 上报支付失败原因
                wx.showModal({
                    title: '提示',
                    showCancel: false,
                    content: error.errMsg,
                })
            }
        }
        wx.hideLoading()
        wx[paymentType](signData);
    }
})